﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CardLogistics.Web.Components;

namespace CardLogistics.Web.MainPage.Purchase.Contract
{
    public partial class Detail : BasePage
    {
        private static readonly CardLogistics.DAL.Purchase.Purchase_ContractData DAL = new DAL.Purchase.Purchase_ContractData();
        private static readonly CardLogistics.DAL.Purchase.Purchase_Contract_detailData DetailDAL = new DAL.Purchase.Purchase_Contract_detailData();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void InitControl()
        {
            string _key = Request.QueryString["id"];
            WebUserControl_AppInfo1.InfoID = _key;
            WebUserControl_AppInfo1.UserID = LoggedUser.ID;
            if (!string.IsNullOrEmpty(_key))
            {
                CardLogistics.Model.Purchase.Purchase_Contract pData = DAL.GetModel(_key);
                SetWebControls(this.Page, pData);
                if (pData != null)
                {
                    //签定日期
                    if (pData.SIGNDATE != null)
                    {
                        SIGNDATE.Text = DateTime.Parse(pData.SIGNDATE.ToString()).ToString("yyyy-MM-dd");
                    }
                    //生效日期
                    if (pData.BEDATE != null)
                    {
                        BEDATE.Text = DateTime.Parse(pData.BEDATE.ToString()).ToString("yyyy-MM-dd");
                    }
                    if (pData.SIGNADDRESS ==null) //如果签定地点为null则页面也隐藏
                    {
                        SIGNADDRESS.Visible = false;
                    }
                    if (pData.CONPIC == null) //如果负责人为null则页面也隐藏
                    {
                        CONPIC.Visible = false;
                    }
                }
                DataTable dt = DetailDAL.GetInfoByConId(pData.ID);//通过合同外键查询合同明细信息

                if (dt != null && dt.Rows.Count > 0)
                {
                    int TotalCardNumber = 0; //卡片数量
                    int TotalSparepartsNumber = 0;//备品数量
                    int TotalPremiumsNumber = 0;//赠品数量
                    decimal Firstsum = 0; //首付款
                    decimal Acceptsum = 0; //验收款
                    decimal Qualitysum = 0; //质保金
                    decimal TotalPrice = 0;//总价格
                    if (dt.Rows[0]["CardAmount"] != DBNull.Value)//卡片数量
                    {
                        TotalCardNumber = Convert.ToInt32(dt.Compute("sum(CardAmount)", ""));
                    }
                    else
                        TotalCardNumber = 0;

                    if (dt.Rows[0]["Spareparts"] != DBNull.Value)//备品数量
                    {
                        TotalSparepartsNumber = Convert.ToInt32(dt.Compute("sum(Spareparts)", ""));
                    }
                    else
                        TotalSparepartsNumber = 0;

                    if (dt.Rows[0]["Premiums"] != DBNull.Value)//赠品数量
                    {
                        TotalPremiumsNumber = Convert.ToInt32(dt.Compute("sum(Premiums)", ""));
                    }
                    else
                        TotalPremiumsNumber = 0;

                    if (dt.Rows[0]["TotalPrice"] != DBNull.Value)//总价格
                    {
                        TotalPrice = Convert.ToDecimal(dt.Compute("sum(TotalPrice)", ""));//总价格
                    }
                    else
                        TotalPrice = 0;
                    //首付款
                    if (dt.Rows[0]["FIRSTPAY"] != DBNull.Value)
                    {
                        Firstsum = Convert.ToDecimal(dt.Compute("sum(FIRSTPAY)", ""));
                    }
                    else
                    {
                        Firstsum = 0;
                    }
                    //验收款
                    if (dt.Rows[0]["ACCEPTANCE"] != DBNull.Value)
                    {
                        Acceptsum = Convert.ToDecimal(dt.Compute("sum(ACCEPTANCE)", ""));
                    }
                    else
                    {
                        Acceptsum = 0;
                    }
                    //质保金
                    if (dt.Rows[0]["QUALITYASSURANCE"] != DBNull.Value)
                    {
                        Qualitysum = Convert.ToDecimal(dt.Compute("sum(QUALITYASSURANCE)", ""));
                    }
                    else
                    {
                        Qualitysum = 0;
                    }
                    DataRow dr = dt.NewRow();
                    dr["BATCHNO"] = "合计";
                    dr["CardAmount"] = TotalCardNumber;
                    dr["Spareparts"] = TotalSparepartsNumber;
                    dr["Premiums"] = TotalPremiumsNumber;
                    dr["Price"] = TotalPrice;
                    dr["FIRSTPAY"] = Firstsum;
                    dr["ACCEPTANCE"] = Acceptsum;
                    dr["QUALITYASSURANCE"] = Qualitysum;
                    dt.Rows.Add(dr);

                    rptDetailList.DataSource = dt;
                    rptDetailList.DataBind();
                }
            }
        }
    }
}